home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1997 December / PC Pro December 1997 CD-Rom coverdisc.iso / symantec / dbAnywh / JAVA.BIN / CLASSES.ZIP / java / net / InetAddress.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-12-14  |  4.3 KB  |  202 lines

  1. package java.net;
  2.  
  3. import java.util.Hashtable;
  4.  
  5. public final class InetAddress {
  6.    private static boolean inCheck;
  7.    String hostName;
  8.    int address;
  9.    int family;
  10.    static Hashtable addressCache;
  11.    static InetAddress unknownAddress;
  12.    static InetAddress anyLocalAddress;
  13.    static InetAddress localHost;
  14.    private static int localHostInitialize;
  15.    private static InetAddress loopbackHost;
  16.  
  17.    InetAddress() {
  18.    }
  19.  
  20.    InetAddress(String var1, byte[] var2) {
  21.       this.hostName = new String(var1);
  22.       this.family = getInetFamily();
  23.       this.address = var2[3] & 255;
  24.       this.address |= var2[2] << 8 & '\uff00';
  25.       this.address |= var2[1] << 16 & 16711680;
  26.       this.address |= var2[0] << 24 & -16777216;
  27.    }
  28.  
  29.    public String getHostName() {
  30.       if (this.hostName == null) {
  31.          try {
  32.             this.hostName = getHostByAddr(this.address);
  33.          } catch (UnknownHostException var1) {
  34.             this.hostName = this.getHostAddress();
  35.          }
  36.       }
  37.  
  38.       return this.hostName;
  39.    }
  40.  
  41.    public byte[] getAddress() {
  42.       byte[] var1 = new byte[]{(byte)(this.address >>> 24 & 255), (byte)(this.address >>> 16 & 255), (byte)(this.address >>> 8 & 255), (byte)(this.address & 255)};
  43.       return var1;
  44.    }
  45.  
  46.    public String getHostAddress() {
  47.       return (this.address >>> 24 & 255) + "." + (this.address >>> 16 & 255) + "." + (this.address >>> 8 & 255) + "." + (this.address & 255);
  48.    }
  49.  
  50.    public int hashCode() {
  51.       return this.address;
  52.    }
  53.  
  54.    public boolean equals(Object var1) {
  55.       return var1 != null && var1 instanceof InetAddress && ((InetAddress)var1).address == this.address;
  56.    }
  57.  
  58.    public String toString() {
  59.       return (this.hostName != null ? this.hostName + "/" : "") + this.getHostAddress();
  60.    }
  61.  
  62.    public static synchronized InetAddress getByName(String var0) throws UnknownHostException {
  63.       if (var0 != null && var0.length() != 0) {
  64.          if (!Character.isDigit(var0.charAt(0))) {
  65.             return getAllByName(var0)[0];
  66.          } else {
  67.             SecurityManager var1 = System.getSecurityManager();
  68.             if (var1 != null && !var1.getInCheck()) {
  69.                var1.checkConnect(var0, -1);
  70.             }
  71.  
  72.             Object var2 = addressCache.get(var0);
  73.             if (var2 == null) {
  74.                try {
  75.                   byte[] var3 = lookupHostAddr(var0);
  76.                   InetAddress[] var4 = new InetAddress[]{new InetAddress(var0, var3)};
  77.                   var2 = var4;
  78.                } catch (UnknownHostException var5) {
  79.                   var2 = unknownAddress;
  80.                }
  81.  
  82.                addressCache.put(var0, var2);
  83.             }
  84.  
  85.             if (var2 == unknownAddress) {
  86.                throw new UnknownHostException(var0);
  87.             } else {
  88.                return ((InetAddress[])var2)[0];
  89.             }
  90.          }
  91.       } else {
  92.          return loopbackHost;
  93.       }
  94.    }
  95.  
  96.    public static synchronized InetAddress[] getAllByName(String var0) throws UnknownHostException {
  97.       Object var1 = null;
  98.       if (var0 == null) {
  99.          throw new UnknownHostException(var0);
  100.       } else {
  101.          SecurityManager var2 = System.getSecurityManager();
  102.          if (var2 != null && !var2.getInCheck()) {
  103.             var2.checkConnect(var0, -1);
  104.          }
  105.  
  106.          Object var3 = addressCache.get(var0);
  107.          if (var3 == null) {
  108.             try {
  109.                byte[][] var4 = lookupAllHostAddr(var0);
  110.                InetAddress[] var5 = new InetAddress[var4.length];
  111.  
  112.                for(int var6 = 0; var6 < var4.length; ++var6) {
  113.                   byte[] var7 = var4[var6];
  114.                   var5[var6] = new InetAddress(var0, var7);
  115.                   InetAddress[] var8 = new InetAddress[1];
  116.                   String var9 = new String(var5[var6].getHostAddress());
  117.                   var8[0] = new InetAddress(var9, var7);
  118.                   addressCache.put(var9, var8);
  119.                }
  120.  
  121.                var3 = var5;
  122.             } catch (UnknownHostException var11) {
  123.                var3 = unknownAddress;
  124.             }
  125.  
  126.             addressCache.put(var0, var3);
  127.          }
  128.  
  129.          if (var3 == unknownAddress) {
  130.             throw new UnknownHostException(var0);
  131.          } else {
  132.             try {
  133.                var1 = ((InetAddress[])var3).clone();
  134.             } catch (CloneNotSupportedException var10) {
  135.                ((Throwable)var10).printStackTrace();
  136.             }
  137.  
  138.             return (InetAddress[])var1;
  139.          }
  140.       }
  141.    }
  142.  
  143.    private static synchronized void setLocalHost() {
  144.       if (localHostInitialize == 0) {
  145.          localHostInitialize = 1;
  146.  
  147.          try {
  148.             localHost = getByName(getLocalHostName());
  149.          } catch (Exception var0) {
  150.             localHost = unknownAddress;
  151.          }
  152.       }
  153.    }
  154.  
  155.    public static InetAddress getLocalHost() throws UnknownHostException {
  156.       if (localHostInitialize == 0) {
  157.          setLocalHost();
  158.       }
  159.  
  160.       if (localHost.equals(unknownAddress)) {
  161.          throw new UnknownHostException();
  162.       } else {
  163.          SecurityManager var0 = System.getSecurityManager();
  164.  
  165.          try {
  166.             if (var0 != null && !var0.getInCheck()) {
  167.                var0.checkConnect(localHost.getHostName(), -1);
  168.             }
  169.          } catch (SecurityException var1) {
  170.             return loopbackHost;
  171.          }
  172.  
  173.          return localHost;
  174.       }
  175.    }
  176.  
  177.    private static native String getLocalHostName() throws UnknownHostException;
  178.  
  179.    private static native void makeAnyLocalAddress(InetAddress var0);
  180.  
  181.    private static native byte[] lookupHostAddr(String var0) throws UnknownHostException;
  182.  
  183.    private static native byte[][] lookupAllHostAddr(String var0) throws UnknownHostException;
  184.  
  185.    private static native String getHostByAddr(int var0) throws UnknownHostException;
  186.  
  187.    private static native int getInetFamily();
  188.  
  189.    static {
  190.       System.loadLibrary("net");
  191.       addressCache = new Hashtable();
  192.       unknownAddress = new InetAddress();
  193.       anyLocalAddress = new InetAddress();
  194.       makeAnyLocalAddress(anyLocalAddress);
  195.       byte[] var0 = new byte[]{127, 0, 0, 1};
  196.       loopbackHost = new InetAddress("localhost", var0);
  197.       String var1 = new String("0.0.0.0");
  198.       InetAddress[] var2 = new InetAddress[]{new InetAddress(var1, unknownAddress.getAddress())};
  199.       addressCache.put(var1, var2);
  200.    }
  201. }
  202.